<?php
class product_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($_id){
        $where=array('ProductID'=>$_id);
        $query=$this->db->get_where("dtb_product", $where); 
        return $query->result();
    }
    function getIn($_lst){
        $query=$this->db
                ->where_in("ProductID",$_lst)
                ->get("dtb_product"); 
        return $query->result();
    }
    function gets(){
        $where=array('Delete'=>NULL);
        $query=$this->db
                ->where($where)
                ->get("dtb_product"); 
        return $query->result();
    }
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert("dtb_product", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function delete($_id){
        $this->db->set('Delete', 'NOW()', FALSE);
        $where=array('ProductID'=>$_id);
        $this->db->where($where);
        $this->db->update("dtb_product"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function retore($_id){
        $this->db->set('Delete', 'NULL', FALSE);
        $where=array('Product_ID'=>$_id);
        $this->db->where($where);
        $this->db->update("dtb_product"); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function update($_id,$params){
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->where('ProductID', $_id);
        $this->db->update("dtb_product", $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return $count;
        return false;
    }
    function binding(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `dtb_product`.*,_type._name as CategoryName
            FROM `dtb_product`
                LEFT JOIN _type ON ( _type._id = `dtb_product`.`Categorys` )
            ";
        $strWhere="
            WHERE  true
            ";
        if($_SESSION["ADP"][CCTRL]["display"]==0){
            $strWhere.=" AND `dtb_product`.`Delete` IS NULL";
        }elseif($_SESSION["ADP"][CCTRL]["display"]==-1){
            $strWhere.=" AND `dtb_product`.`Delete` IS NOT NULL";
        }
        $strOrderBy="Order By `dtb_product`.`Insert` DESC ";
        $strGroupBy="";
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usingLimit"=>true,
            "fields"=>array(
                "CategoryName"=>"_type._name"
            ),
            "datefields"=>array()
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function getToken($q=""){
        $where=array("Delete"=>null,"Status"=>"Public");  
        $query=$this->db
                ->select("ProductID as id,ProductID,ProductName,Image,UnitPrice")
                ->like('ProductName', $q)
                ->get_where('dtb_product', $where); 
        return $query->result();
    }
}
?>
